<?php
/**
 * 体检中心-体检项目
 *
 * User: kaikaic
 * Date: 2018/10/24
 * Time: 15:59
 */

namespace app\exam\service;

use service\BasicService;
use think\Db;

class MecItemService extends BasicService {

    public function init() {
        $this->dataTable = ['ExamItemClass', 'ExamItem'];
    }

    public function listClass() {
        $records = $this->listAll();

        $classList = array();
        for ($i=0; $i<count($records); $i++) {
            $temp = $records[$i];

            $classList[$temp['id']] = $temp['name'];
        }

        return $classList;
    }

    /**
     * 按体检机构列出项目分类信息
     */
    public function listUnderMec() {
        $records = $this->listAll();

        $dataList = array();
        for ($i=0; $i<count($records); $i++) {
            $tmp = $records[$i];

            if (!isset($dataList[$tmp['mec_id']])) {
                $dataList[$tmp['mec_id']] = array();
            }

            $dataList[$tmp['mec_id']][] = array('id'=>$tmp['id'], 'name'=>$tmp['name']);
        }

        return $dataList;
    }

    /**
     * 体检项目
     */
    public function listClassItems() {
        $classRecords = $this->listAll();
        $itemRecords = $this->listAll(1);

        $itemArray = array();
        for ($i=0; $i<count($classRecords); $i++) {
            $temp = $classRecords[$i];
            $temp['items'] = array();

            $itemArray[$temp['id']] = $temp;
        }

        for ($i=0; $i<count($itemRecords); $i++) {
            $classID = $itemRecords[$i]['class_id'];

            $itemArray[$classID]['items'][] = $itemRecords[$i];
        }

        return array_values($itemArray);
    }

    /**
     * 列出体检中心所有体检项目
     */
    public function listItemsByMec($mecID) {
        $where = array('status'=>1, 'mec_id'=>$mecID);
        $classRecords = Db::name($this->dataTable[0])->where($where)->select();

        $where = array('status'=>1, 'mec_id'=>$mecID);
        $itemRecords = Db::name($this->dataTable[1])->where($where)->select();

        $itemArray = array();
        for ($i=0; $i<count($classRecords); $i++) {
            $temp = $classRecords[$i];
            $temp['items'] = array();

            $itemArray[$temp['id']] = $temp;
        }

        for ($i=0; $i<count($itemRecords); $i++) {
            $classID = $itemRecords[$i]['item_set_id'];

            $itemArray[$classID]['items'][] = $itemRecords[$i];
        }

        return array_values($itemArray);
    }

    /**
     * 生成体检机构-默认体检项目分类
     */
    public function initDefaultClass($mecID) {
        $params = array();
        $params['name'] = "默认分类";
        $params['mec_id'] = $mecID;
        $params['is_default'] = 1;
        $params['sort'] = 1;
        $params['status'] = 1;
        $params['create_time'] = time();

        return $this->insert($params);
    }

    /**
     * 根据ID数组列出体检项目
     */
    public function listItemByIDs($idArray) {
        return Db::name($this->dataTable[1])->whereIn('id', $idArray)->order('item_set_id asc')->select();
    }

    public function listAll($tableIndex=0) {
        $where = array('status'=>1);
        return Db::name($this->dataTable[$tableIndex])->where($where)->select();
    }

    /**
     * 当前排序最大值,新纪录自动+1
     */
    public function getMaxSort($tableIndex=0) {
        return Db::name($this->dataTable[$tableIndex])->max('sort');
    }
}

